package cn.lx.singleton;

import java.lang.reflect.Constructor;

/**
 * @author lixin
 * @Classname SingletonFactory
 * @Description TODO
 * @Date 2019/10/4 上午10:53
 */
public class SingletonFactory {
    private static Singleton singleton;

    static {
        try {
            Class<?> aClass = Class.forName(Singleton.class.getName());
            // 获取无参构造
            Constructor constructor = aClass.getDeclaredConstructor();
            // 设置无参构造可被访问
            constructor.setAccessible(true);
            singleton = (Singleton) constructor.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static Singleton getSingleton() {
        return singleton;
    }
}
